//
//  LBWUserAccount.swift
//  weibo
//
//  Created by Limonwei on 15/12/7.
//  Copyright © 2015年 heima. All rights reserved.
//
//用户账户模型  字典转模型

import UIKit

class LBWUserAccount: NSObject {
    
    //用户id
    var uid: String?
    
    //过期时间,单位秒 --JSON里面的值没有用,引号引过来
    var expires_in: NSTimeInterval = 0 {
        
        didSet{
            //在外界给过期时间赋值的时候,就可以去计算出来什么时候过期
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
            
           printLog(expiresDate)
        }
        
    }
    //代表当前账号是什么时候过期 ,accesstoken获取的那一刻加上过期的秒数
    var expiresDate:NSDate?
    
    //用于调用access_token
    var access_token: String?
    
    //access_token的生命周期
    var remind_in: String?
    
    //用户的头像地址
    var avatar_large: String?
    
    //用户的昵称
    var screen_name: String?
    
    //KVC构造函数 所有的的类都继承AnyObject这个协议
    init(dict: [String: AnyObject]) {
        super.init()
        
        //遍历字典,调用setValue:forKey给对象的属性赋值
        setValuesForKeysWithDictionary(dict)
    }
    
    //赋值的时候没找到字典里面对应的属性,会调用此方法,默认抛出一个`NSUnknownKeyException` 异常
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //重写description 的属性
    override var description: String {
        
        //传入一键数组
        let keys = ["uid", "expires_in", "access_token", "avatar_large", "screen_name"]
        
        //底层会去找这个键对应的value,并生成一个dictionary
        return dictionaryWithValuesForKeys(keys).description
    }
    
    //归档的协议方法
    
    //从文件里读取对象,解档
    required init(coder aDecoder: NSCoder) {
        
        uid = aDecoder.decodeObjectForKey("uid") as? String
        
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        
    }
    
    //把对象保存成二进制文件,归档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(uid, forKey: "uid")
        
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    
    //    // 归档当前对象
    //    func saveAccount() {
    //        // 1. 获取路径
    //        let file = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("useraccount.archive")
    //        printLog(file)
    //        // 2. 归档
    //        NSKeyedArchiver.archiveRootObject(self, toFile: file)
    //    }
    //
    //    // 解档
    //    class func userAccount() -> HMUserAccount? {
    //        // 1. 获取路径
    //        let file = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("useraccount.archive")
    //        // 2. 解档
    //        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? HMUserAccount
    //        return result
    //    }

}











